<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            border-collapse: collapse;
            border: 2px solid green;
            margin-top: 10px;
        }

        tr,
        th,
        td {
            border: 2px solid green;
        }

        th {
            padding: 10px 25px;
        }

        tr th:nth-of-type(4) {
            padding: 10px 60px;
        }

        tr td {
            text-align: center;
            padding: 10px 20px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div @keyup.enter="add">
            <input type="text" placeholder="请输入姓名" v-model="obj.n">
            <input type="text" placeholder="请输入性别" v-model="obj.s">
            <input type="text" placeholder="请输入手机号" v-model="obj.t">
            <input type="text" placeholder="请输入年龄" v-model="obj.a">
            <button @click="add()">添加</button>
        </div>
        <table>
            <tr>
                <th>学号</th>
                <th>姓名</th>
                <th>性别</th>
                <th>联系方式</th>
                <th @click="flag=!flag;sortNum()">年龄
                    <span v-if="flag">&darr;</span>
                    <span v-else>&uarr;</span>
                </th>
            </tr>
            <tr v-for="(item,index) in infoArr">
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>{{item.sex}}</td>
                <td>{{item.tel}}</td>
                <td>{{item.age}}</td>
            </tr>
        </table>
    </div>

    <script src="./js/vue.js"></script>
    <script>
        var app = new Vue({
            el: "#app",
            data: {
                infoArr: [
                    { "id": 1, "name": "小甜甜", "sex": "女", "tel": "13412345678", "age": 28 },
                    { "id": 2, "name": "呜呜", "sex": "男", "tel": "13412345678", "age": 25 },
                    { "id": 3, "name": "嗷嗷", "sex": "男", "tel": "13412345678", "age": 26 }
                ],
                obj: { "n": "", "s": "", "t": "", "a": "" },
                num: 4,
                flag: true,
            },
            methods: {
                add() {
                    if (this.obj.n == "") {
                        alert("请将数据填写完整！！！");
                        return;
                    }
                    this.infoArr.push({ "id": this.num, "name": this.obj.n, "sex": this.obj.s, "tel": this.obj.t, "age": this.obj.a });
                    console.log(this.obj.n);
                    this.obj.n = "";
                    this.obj.s = "";
                    this.obj.t = "";
                    this.obj.a = "";
                    this.num++;
                },
                sortNum() {
                    this.infoArr.sort((a, b) => {
                        return this.flag ? a.age - b.age : b.age - a.age;
                    })
                },
            }
        })
    </script>
</body>

</html>